package cn.aifeix.lxyc.service.impl;

import cn.aifeix.lxyc.domain.Order;
import cn.aifeix.lxyc.domain.ServicePlan;
import cn.aifeix.lxyc.dto.UserOrderDto;
import cn.aifeix.lxyc.enums.GlobalErrorCodeEnum;
import cn.aifeix.lxyc.mapper.OrderMapper;
import cn.aifeix.lxyc.service.IOrderService;
import cn.aifeix.lxyc.utils.AssertUtil;
import cn.aifeix.lxyc.utils.CodeGenerateUtils;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author director
 * @since 2022-12-10
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    /**
     * 下单
     * @param userOrderDto
     */
    @Override
    public void placeAnOrder(UserOrderDto userOrderDto) {
        ServicePlan servicePlan = userOrderDto.getServicePlan();
        Integer payType = userOrderDto.getPayType();
        Date bookedTime = userOrderDto.getBookedTime();
        AssertUtil.isNotNull(servicePlan, GlobalErrorCodeEnum.ERROR);
        BigDecimal laborCost = servicePlan.getLaborCost();
        BigDecimal productPrice = servicePlan.getProductPrice();
        Long serviceId = servicePlan.getServiceId();
        Order order = new Order();
        order.setBookedTime(bookedTime);
        order.setCreateTime(new Date());
        String orderSn = CodeGenerateUtils.generateOrderSn(1L);
        order.setOrderNo(orderSn);
        order.setPayType(payType);
        order.setStatusOrder(0);
        order.setUserId(1L);
        BigDecimal add = laborCost.add(productPrice);
        order.setTotalAmount(add);
        insert(order);
        // 调用支付服务 完成支付后修改状态和title
        String title = "购买服务：" + serviceId + ",共支付：" + add + "元";
        order.setTitle(title);
        // 如果支付成功
        order.setStatusOrder(1);
        updateById(order);
    }
}

















